package com.wateray.ipassbook.kernel.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.ibatis.dao.client.DaoManager;
import com.wateray.ipassbook.ApplicationConfig;
import com.wateray.ipassbook.commom.CommomTool;
import com.wateray.ipassbook.domain.Passbook;
import com.wateray.ipassbook.domain.PassbookDetail;
import com.wateray.ipassbook.domain.form.PassbookForm;
import com.wateray.ipassbook.kernel.event.IPassbookEvent;
import com.wateray.ipassbook.kernel.event.IPassbookEventType;
import com.wateray.ipassbook.kernel.event.IPassbookObserver;
import com.wateray.ipassbook.persistence.DaoConfig;
import com.wateray.ipassbook.persistence.iface.PassbookDao;

public class PassbookService {
	private PassbookDao passbookDao;
	private DaoManager daoMgr;

	public PassbookService() {
		daoMgr = DaoConfig.getDaoManager();
		this.passbookDao = (PassbookDao) daoMgr.getDao(PassbookDao.class);
	}

	public PassbookService(DaoManager daoMgr, PassbookDao passbookDao) {
		this.daoMgr = daoMgr;
		this.passbookDao = passbookDao;
	}

	public List<Passbook> getPassbookList() {
		return passbookDao.getPassbookList();
	}

	public Passbook getPassbookById(String passbookid) {
		return passbookDao.getPassbookById(passbookid);
	}
	public PassbookDetail getDefaultPassbookDetailById(String passbookid) {
		Passbook pb = passbookDao.getPassbookById(passbookid);
		
		if(pb == null){
			return null;
		}
		
		List<PassbookDetail> list = pb.getPassbookList();
		if(list.size()>0){
			return list.get(0);
		}
		
		return null;
	}
	public int insertPassbook(Passbook passbook) {
		return passbookDao.insertPassbook(passbook);
	}

	/**
	 * */
	public int insertPassbookDetail(Passbook passbook,
			PassbookDetail passbookDetail) {
		int result = 0;
		try {
			daoMgr.startTransaction();
			Passbook getPassbook = passbookDao.getPassbookById(passbook
					.getPassbookId());
			if (getPassbook == null) {
				// maxId
				String maxId = passbookDao.getPassbookMaxId();
				passbook.setPassbookId(CommomTool.plusOneFillZero(maxId, 10));

				result = passbookDao.insertPassbook(passbook);
			}
			// subMaxId
			String subMaxId = passbookDao.getPassbookDetailMaxId(passbook
					.getPassbookId());
			passbookDetail.setPassbookSubId(CommomTool.plusOneFillZero(
					subMaxId, 0));

			// maxId
			passbookDetail.setPassbookId(passbook.getPassbookId());

			result += passbookDao.insertPassbookDetail(passbookDetail);
			daoMgr.commitTransaction();
		} finally {
			daoMgr.endTransaction();
		}
		return result;
	}

	public int insertPassbookDetail(PassbookDetail passbookDetail) {
		return passbookDao.insertPassbookDetail(passbookDetail);
	}
	
	public int updatePassbook(Passbook passbook) {
		return passbookDao.updatePassbook(passbook);
	}
	public int updatePassbookDetail(PassbookDetail passbookDetail) {
		
		return passbookDao.updatePassbookDetail(passbookDetail);
	}

	public List getPassbookFormList() {	
		return passbookDao.getPassbookFormList(ApplicationConfig.getLang());
	}
	public int updatePassbookDetailBalance(PassbookDetail passbookDetail){
		return passbookDao.updatePassbookDetailBalance(passbookDetail);
	}
	
	public int updateBalanceByTransfer(PassbookDetail from, PassbookDetail to){
		int result = 0;
		try {
			daoMgr.startTransaction();
			this.updatePassbookDetailBalance(from);
			this.updatePassbookDetailBalance(to);	
			
			daoMgr.commitTransaction();
			// invoke
			IPassbookObserver.getInstance().notifyEvents(
					new IPassbookEvent(this,
							IPassbookEventType.PassbookTableModifyEvent));
		} finally {
			daoMgr.endTransaction();
		}
		
		return result;
	}
	
	public PassbookDetail getPassbookDetailById(String passbookId, String passbookSubId){
		Map<String, String> map = new HashMap<String, String>();
		map.put("passbookId", passbookId);
		map.put("passbookSubId", passbookSubId);
		
		return passbookDao.getPassbookDetailById(map);
	}
	public static void main(String[] args) {
		PassbookService service = new PassbookService();
		// List<Passbook> list;
		// list = service.getPassbookList();
		// Iterator<Passbook> e = list.iterator();
		// while (e.hasNext()) {
		// Passbook entity = (Passbook) e.next();
		// System.out.println(" EntityId=" + entity.getEntityId()
		// + " EntityName=" + entity.getEntityName());
		// }

		 Passbook passbook =
		 service.passbookDao.getPassbookById("0000000019");
		// if(passbook == null)
		// System.out.println("passbook is null.");
		// else
		// System.out.println("passbookId=" + passbook.getEntityId());
//
//		Passbook passbook = new Passbook();
//		PassbookDetail passbookDetail = new PassbookDetail();
//		List list = new ArrayList();
//		list.add(passbookDetail);
//		passbook.setPassbookList(list);
//
//		passbook.setPassbookName("系统测试");
//		passbookDetail.setBalance(100);
//		passbookDetail.setCapital(100);
//		passbookDetail.setCurrencyId("100");
//
//		service.insertPassbookDetail(passbook, passbookDetail);
//		passbook.setPassbookName("中和测试");
//		PassbookDetail passbookDetail = passbook.getPassbookList().get(0);
//		passbookDetail.setBalance(888888);
//		service.updatePassbook(passbook);
//		service.updatePassbookDetail(passbookDetail); 
		
		 service.getPassbookFormList();
	}
}
